package top.xmj.controller;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.xmj.domain.FriendBO;
import top.xmj.service.IFriendService;
import top.xmj.vo.ResultVO;

import java.util.List;

/**
 * @author 冰咖啡
 * Date:2022/12/13 19:28
 * Description:
 */
@RestController
@RequestMapping("/friend")
@ApiModel("好友操作")
public class FriendController {

    @Autowired
    private IFriendService friendService;

    @GetMapping("/list")
    @ApiOperation("获取好友")
    public ResultVO getListByUserId(@RequestParam(name = "userId") String userId) {
        List<FriendBO> friends = friendService.getListByUserId(userId);
        return new ResultVO(200,"获取成功",friends);
    }

    @PostMapping("/friend")
    @ApiOperation("添加好友")
    public ResultVO addFriend(@RequestParam(name = "userId") String userId,
                              @RequestParam(name = "friendId") String friendId,
                              @RequestParam(name = "applyMsg") String applyMsg)  {
        boolean res = friendService.addFriend(userId, friendId, applyMsg);
        if(!res){
            return new ResultVO(505,"发送异常",null);
        }
        return new ResultVO(200,"操作成功",null);
    }

    @DeleteMapping("/friend")
    @ApiOperation("删除好友")
    public ResultVO deleteFriend(@RequestParam(name = "userId") String userId,
                                 @RequestParam(name = "friendId") String friendId){
        boolean res = friendService.deleteFriend(userId, friendId);
        if(!res){
            return new ResultVO(505,"发送异常",null);
        }
        return new ResultVO(200,"操作成功",null);
    }

    @PostMapping("/agree")
    @ApiOperation("同意添加好友")
    public ResultVO agreeAddFriend(@RequestParam(name = "userId") String userId,
                                   @RequestParam(name = "friendId") String friendId){
        boolean res = friendService.agreeAddFriend(userId, friendId);
        if(!res){
            return new ResultVO(505,"发送异常",null);
        }
        return new ResultVO(200,"操作成功",null);
    }

    @PostMapping("/reject")
    @ApiOperation("拒绝添加")
    public ResultVO rejectAddFriend(@RequestParam(name = "userId") String userId,
                                    @RequestParam(name = "friendId") String friendId){
        boolean res = friendService.rejectAddFriend(userId, friendId);
        if(!res){
            return new ResultVO(505,"发送异常",null);
        }
        return new ResultVO(200,"操作成功",null);
    }


    @GetMapping("/friend")
    @ApiOperation(value = "根据用户名查找好友")
    public ResultVO getFriendByUserName(@RequestParam(value = "username") String username){
        FriendBO friend = friendService.findFriendByUserName(username);
        return new ResultVO(200,"获取成功",friend);
    }
}
